--
-- Author: shunguo.chen
-- Date: 2018-11-19 15:00:13
--
-- TeamTargetSecretRender 组队查找器秘境菜单
--

local Skill = require("app.public.dao.Skill")
local UIUtil = require("app.utils.UIUtil")
local SkillSimpleRender = require("app.widget.renders.SkillSimpleRender")

local ClassRef = app.mvc.createRenderClass(...)

local NEXT_LAYER_STR = L("lua_code_text_34")
local MAX_LAYER_STR = L("lua_code_text_343")

--data = TeamTargetVo
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TeamTargetSecretNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())

    self._tipText = self._ui["Panel/text"]

    local topPanel = self._ui.Panel_top
    self._layerText = topPanel:getChildByName("num")
    self._nameText = topPanel:getChildByName("name")
    self._tlt = topPanel:getChildByName("tlt")
    self._tlt:setString(L("SecretDungeon"))

    local skillRenders = {}
    for i=1,3 do
        local render = SkillSimpleRender.create(topPanel:getChildByName("buff_" .. i))
        render:setTipEnable(true)
        skillRenders[i] = render
    end
    self._skillRenders = skillRenders

    self._nodes = {
        self._ui["Panel/node_1"],
        self._ui["Panel/node_2"],
        self._ui["Panel/node_3"],
    }
    self._nodes[1]:findChild("Panel/icon"):loadTexture(Res.teamUIPath .. "secret_trophy_3.png", 1)
    self._nodes[2]:findChild("Panel/icon"):loadTexture(Res.teamUIPath .. "secret_trophy_2.png", 1)
    self._nodes[3]:findChild("Panel/icon"):loadTexture(Res.teamUIPath .. "secret_trophy_1.png", 1)

    self.tipsNode = {mUINode=UIHandler:getSystemUi()}
    for i, v in ipairs(self._nodes) do        
        v:findChild("Panel"):setTouchEnabled(true)
        v:findChild("Panel"):addClickEventListener(function(sender)
            UIUtil.showTips(self.tipsNode, v:findChild("Panel"), {
                name=L("secretDungeontips1"),
                desc1=L("secretDungeontips2"),
            })
        end)
    end

    self:refresh()
end

function ClassRef:refresh()
    local info = SecretManager.info
    if self._data and info then
        self._nameText:setString(SecretManager:getCurrentCopyName())

        if info.layer >= info.initLayer + SecretManager.maxLayer then
            self._layerText:setString(info.layer - 1)
            self._tipText:setString(MAX_LAYER_STR)
        else
            self._layerText:setString(info.layer)
            self._tipText:setString(string.format(NEXT_LAYER_STR, info.layer + 1))
        end

        for i, v in ipairs(info.npcStartBuffIds) do
            local skill = Skill.new()
            skill:read({uid=v})
            skill.getIcon = function(self)
                if not self.icon then
                    self.icon = GD:getImagePath(Res.buffIconPath, self.db.icon)
                end
                return self.icon
            end
            self._skillRenders[i]:setData(skill)
        end

        local grades = SecretManager:getGradesFromLayer(info.layer)
        local curGrade = protobuf.enum_id("pb_secretDungeon.REWARD", info.grade)
        if grades then
            for i = 1, 3 do 
                local node = self._nodes[i]
                if i > curGrade then
                    node:findChild("Panel/tlt"):setString(grades[i].title)
                    node:findChild("Panel/time"):setString(grades[i].hmstr)
                    node:findChild("Panel/bg/num"):setString(grades[i].str)
                else 
                    node:findChild("Panel/tlt"):hide()
                    node:findChild("Panel/time"):hide()
                    node:findChild("Panel/bg"):hide()

                    node:findChild("Panel/icon_over"):show()
                    node:findChild("Panel/over"):show()
                end
            end
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
